<%@page isELIgnored="false" pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
	<script type="text/javascript">
		$(function () {
            $('#myArticleDatagrid').datagrid({
                url:'${pageContext.request.contextPath}/article/selectCmfzArticleDatagrid',
                method:"get",
                pagination:true,
                columns:[[
                    //一旦加上复选框属性，页面展示不出来效果，刷新后表格对不齐
					{field:'11',title:'11',checkbox:true},
                    {field:'articleId',title:'文章编号',width:100,align:"center"},
                    {field:'articleName',title:'文章标题',width:200,align:"center"},
                    {title:"文章配图",field:"articleImage" ,
						formatter: function (value, row, index) {
                            return "<img width='60px' height='45px' src='${pageContext.request.contextPath}" + row.articleImage + "'/>";
                        }},
                    {field:'articleDate',title:'上传日期',width:150,align:"center"},
                    {field:'articleContent',title:'文章内容',width:300,align:"center"},
                    {field:'guruId',title:'所属上师',width:150,align:"center",
                        formatter: function (value, row, index) {
                            return row.cmfzGuru.guruName;
                        }}
                ]],
                toolbar: '#myArticleTooBar',
                onDblClickRow:function(index, row){
                    $("#updateArticleForm").dialog("open");
                    $("#updateId").val(row.articleId);
                    $("#updateArticleName").val(row.articleName);
                    $("#updateDate").val(row.articleDate);


                    // 此处上师编号无法显示
                    $("#updateGuruId").val(row.guruId);


                    $("#updateContent").val(row.articleContent);
                }
            });


            //隐藏修改文章界面
            $('#updateArticleForm').dialog({
                title: '修改界面',
                width: 400,
                height: 400,
                closed: true,
                modal:true,
                draggable:true,
                resizable:true
            });

            //隐藏添加文章界面
            $('#myAddArticleDialog').dialog({
                title: '添加界面',
                width: 400,
                height: 400,
                closed: true,
                modal:true,
                draggable:true,
                resizable:true
            });
        });
		// 打开添加会话框
		function toAddArticleDialog() {
            $('#myAddArticleDialog').dialog("open");
        }
        // 执行添加操作
		function doAddArticleForm() {
            $('#addArticleForm').form('submit', {
				url:"${pageContext.request.contextPath}/article/addArticle",
                onSubmit: function(){
                // do some check
                return true;
            },
            success:function(data){
				    alert(data);
                //后台执行成功之后的回调函数
                var data = JSON.parse(data);

                if(data.isAdd){
                    $("#myAddArticleDialog").dialog("close");
                    $("#myArticleDatagrid").datagrid("reload");
                }else{
                    alert("添加失败");
                }
            }
        });
	}

        //执行修改图书操作
        function doupdateArticleForm() {

            var articleId=$("#updateId").val();
            var articleName=$("#updateArticleName").val();
            //var articleImage=$("#updatePic").val();
            var articleContent=$("#updateContent").val();
            var guruId=$("#updateGuruId").val();
            var articleDate=$("#updateDate").val();

            //测试要提交的数据是否正确
            //alert(articleId+" "+articleName+" "+articleImage+" "+articleContent+" "+guruId+" "+articleDate);
            $.ajax({
                url:"${pageContext.request.contextPath}/article/updateArticle",
				method:"post",
                data:{"articleId":articleId,"articleName":articleName,"articleContent":articleContent,"guruId":guruId,"articleDate":articleDate},
                success:function(data){
                    //如果修改成功，关闭对话框、刷新datagrid
                    if(data.isUpdate){
                        $("#updateArticleForm").dialog("close");
                        $("#myArticleDatagrid").datagrid("reload");
                    }else{
                        alert("修改失败，请确认")
                    }
                }
            })
        }

        // 执行删除选中文章操作
        function doDeleteSomeArticles() {
            var allSelectionRows=$("#myArticleDatagrid").datagrid("getSelections");
            if(allSelectionRows.length==0){
                alert("请选择要删除的数据！");
            }else{
                var isConfirm=confirm("请确认是否删除？");
                if(isConfirm){
                    var allIds=new Array(allSelectionRows.length);
                    for(var i=0;i<allIds.length;i++){
                        allIds[i]=allSelectionRows[i].articleId;
                    }
                    //发送ajax请求
                    $.ajax({
                        url:"${pageContext.request.contextPath}/article/deleteSomeArticles",
                        data:"ids="+allIds,
                        success:function(data){
                            //删除成功，刷新页面
                            if(data.isDelete){
                                $("#myArticleDatagrid").datagrid("reload");
                            }else{
                                alert("删除失败，请确认！");
                            }
                        }
                    })
                }
            }
        }

        // 重置索引库
        function doResetSearch() {
            //发送ajax请求
            $.ajax({
                url:"${pageContext.request.contextPath}/article/resetIndex",
                success:function(data){
                    //重置索引库成功，刷新页面
                    if(data.isReset){
                        alert("重置索引库成功！请确认！！！");
                    }else{
                        alert("重置索引库失败！请确认！！！");
                    }
                }
            })
        }

        // 执行全文检索功能
        function doDoSearchArticles() {
			var keyWord=$("#keyWord").val();

            $('#myArticleDatagrid').datagrid({
                url:'${pageContext.request.contextPath}/article/selectArticleByKeyWord',
                queryParams: {
                    keyWord:keyWord
                },
                method:"get",
                pagination:true,
                columns:[[
                    //一旦加上复选框属性，页面展示不出来效果，刷新后表格对不齐
                    {field:'11',title:'11',checkbox:true},
                    {field:'articleId',title:'文章编号',width:100,align:"center"},
                    {field:'articleName',title:'文章标题',width:200,align:"center"},
                    {title:"文章配图",field:"articleImage" ,
                        formatter: function (value, row, index) {
                            return "<img width='60px' height='45px' src='${pageContext.request.contextPath}" + row.articleImage + "'/>";
                        }},
                    {field:'articleDate',title:'上传日期',width:150,align:"center"},
                    {field:'articleContent',title:'文章内容',width:300,align:"center"}
                ]],
                toolbar: '#myArticleTooBar',
                onDblClickRow:function(index, row){
                    $("#updateArticleForm").dialog("open");
                    $("#updateId").val(row.articleId);
                    $("#updateArticleName").val(row.articleName);
                    $("#updateDate").val(row.articleDate);


                    // 此处上师编号无法显示
                    $("#updateGuruId").val(row.guruId);


                    $("#updateContent").val(row.articleContent);
                }
            });
        }

	</script>
	<div id="myArticleTooBar">
		<a class="easyui-linkbutton" onclick="toAddArticleDialog()">添加</a>
		<a class="easyui-linkbutton" onclick="doDeleteSomeArticles()">批量删除</a>
		<a class="easyui-linkbutton" onclick="doResetSearch()">重置索引库</a>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="text" id="keyWord"/><a class="easyui-linkbutton" onclick="doDoSearchArticles()">全文检索</a>
	</div>
	<table id="myArticleDatagrid"></table>

<%-------------------------------添加文章页面开始------------------%>
	<div id="myAddArticleDialog">
		<form id="addArticleForm" method="post" enctype="multipart/form-data">
			<table align="center">
				<tr align="center">
					<td>文章标题</td>
					<td><input  name="articleName" type="text"/></td>
				</tr>
				<tr align="center">
					<td>文章配图</td>
					<td>
						<input name="mf" type="file"/>
					</td>
				</tr>
				<tr align="center">
					<td>上传日期</td>
					<td>
						<input class="Wdate" type="text" name="articleDate" onClick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'2008-03-08 11:30:00',maxDate:'2100-03-10 20:59:30'})">
					</td>
				</tr>
				<tr align="center">
					<td>文章作者</td>
					<td><input  name="guruId" type="text"/></td>
				</tr>
				<tr align="center">
					<td>文章内容</td>
					<td><textarea  name="articleContent"></textarea></td>
				</tr>
				<tr align="center">
					<td colspan="2">
						<input type="button" onclick="doAddArticleForm()" value="添加"/>
					</td>
				</tr>
			</table>
		</form>
	</div>

<%------------------------------------添加文章页面结束------------------------%>



	<%-------------------------------修改文章页面开始------------------%>
	<form id="updateArticleForm" method="post"  enctype="multipart/form-data">
		<table align="center">
			<tr align="center" hidden>
				<td>文章编号</td>
				<td><input id="updateId" name="articleId"/></td>
			</tr>
			<tr align="center">
				<td>文章标题</td>
				<td><input id="updateArticleName" name="articleName"/></td>
			</tr>
			<%--需要骚操作来完成，加载图片路径，也就是图片回显--%>
			<tr align="center">
				<td>文章图片</td>
				<td><input id="updatePic" name="umf" type="file"/></td>
			</tr>
			<tr align="center">
				<td>上师编号</td>
				<td><input id="updateGuruId" name="guruId"/></td>
			</tr>
			<tr align="center">
				<td>创建日期</td>
				<td>
					<input class="Wdate" id="updateDate" name="articleDate" type="text"  onClick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'2008-03-08 11:30:00',maxDate:'2100-03-10 20:59:30'})">
				</td>
			</tr>
			<tr align="center">
				<td>文章内容</td>
				<td><textarea id="updateContent" name="articleContent" style="height: 150px"></textarea></td>
			</tr>
			<tr align="center">
				<td colspan="2">
					<input type="button" onclick="doupdateArticleForm()" value="修改"/>
				</td>
			</tr>
		</table>
	</form>
	<%------------------------------------修改文章页面结束------------------------%>
